package com.zzzs.threaddemo;

import lombok.SneakyThrows;

import java.util.concurrent.locks.LockSupport;

/**
 * @author zhu_shuai
 * @since 9/2
 */
public class Demo03_LockSuport {

    static Thread threadA;
    static Thread threadB;
    static Thread threadC;

    public static void main(String[] args) {
        MyRunableA a = new MyRunableA("A");
        MyRunableB b = new MyRunableB("B");
        MyRunableC c = new MyRunableC("C");

        threadA = new Thread(a);
        threadB = new Thread(b);
        threadC = new Thread(c);
        threadA.start();
        threadB.start();
        threadC.start();

        LockSupport.unpark(threadA);


    }

    static class MyRunableA implements Runnable{

        private String name;
        public MyRunableA(String name) {
            this.name = name;
        }

        @SneakyThrows
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                LockSupport.park(threadA);
                System.out.print("A");
                LockSupport.unpark(threadB);
            }
            // System.out.printf("thread - %s %n", this.name);
        }
    }

    static class MyRunableB implements Runnable{

        private String name;
        public MyRunableB(String name) {
            this.name = name;
        }

        @SneakyThrows
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                LockSupport.park(threadB);
                System.out.print("B");
                LockSupport.unpark(threadC);
            }
            // System.out.printf("thread - %s %n", this.name);
        }
    }

    static class MyRunableC implements Runnable{

        private String name;
        public MyRunableC(String name) {
            this.name = name;
        }

        @SneakyThrows
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                LockSupport.park(threadC);
                System.out.print("C");
                LockSupport.unpark(threadA);
            }
            // System.out.printf("thread - %s %n", this.name);
        }
    }
}
